import store from "@/store/index.js"
import request from "@/z/function/request.js"

const state = () => ({
	upw: {},
	upwtypes: ['0', '12', '18', '19', '20', '4', '3', '7'],
	upwuUser: '',
})
const getters = {
	upw: (state) => state.upw,
	b_index: (state) => state.upw.b_index == 1,
	b_info: (state) => state.upw.b_info == 1,
	b_rz: (state) => state.upw['b_rz'] == 1,
	b_llf: (state) => state.upw['b_llf'] == 1,
	b_dyfrz: (state) => state.upw['b_dyfrz'] == 1,
	b_glf: (state) => state.upw['b_glf'] == 1,
	fyfalg: () => {
		let user = store.state.user.token
		return user.utype == 15 && user.udj == 1 ? false : true
	},
	b_fr: (state) => state.upw['b_fr'] == 1,
	b_bxr: (state) => state.upw['b_bxr'] == 1,
	b_cardcz: (state) => state.upw['b_cardcz'] == 1,
	b_sbdel: (state) => state.upw['b_sbedt'] == 1 && state.upw['b_sbdel'] == 1,
	b_sbedt: (state) => state.upw['b_sbedt'] == 1,
	b_month: (state) => state.upw['b_month'] == 1,
	// 物联网卡管理卡管理添加修改功能
	b_usc: (state) => state.upw['b_usc'] == 1,

	b_cardsetting: () => {
		//卡编辑权限
		if (store.state.user.token.utype == 20) {
			return false
		}
		return true
	},
	usearchedt: () => {
		//用户修改权限
		if (store.state.user.token.utype == 3) {
			return false
		}
		return true
	},
	EndCD: () => {
		//充电退款权限
		if (
			store.state.user.token.utype == 3 ||
			store.state.user.token.utype == 4 ||
			(store.state.user.token.utype == 0 && store.state.user.token.udj == 0)
		) {
			return true
		}
		return true
	},
	b_dyfrzedt: () => {
		//站点分润修改权限
		if (store.state.user.token.utype == 3) {
			return false
		}
		return true
	},
}
const mutations = {
	setUpw(state, upw = {}) {
		state.upw = upw
	},
	setupwuUser(state, uUser = {}) {
		console.log(uUser, 'uUseruUser')
		state.upwuUser = uUser
	},
}
const actions = {
	async getUpw({
		state,
		commit
	}) {
		let user = store.state.user.USearch
		// console.log(user, "useruser");
		if (state.upwuUser == user.uUser) {
			return uni.$u.deepClone(state.upw)
		}
		let mrUPW = {
			b_dl: '1',
			b_jrsl: '1',
			b_zsl: '1',
			b_card: '1',
			b_msg: '1',
			b_cardxn: '1',
			b_cardcz: '1',
			b_sbedt: '1',
			b_tjcd: '1',
			b_tjcw: '1',
			b_tjzz: '1',
			b_user: '1',
			b_fr: '1',
			b_llf: '1',
			b_tjccz: '1',
			b_sbdel: '1',
		}
		let UPW = {}
		if (user.utype == 7) {
			mrUPW.b_sbedt = '0'
		}
		if (
			!state.upwtypes.find((e) => {
				return e == user.utype
			})
		) {
			UPW = {
				...mrUPW,
				b_bxr: UPW.b_bxr || '0'
			}
		} else {
			UPW = await request('/UPW', {
				uUser: user.uUser
			})
			UPW = UPW.length == 0 ? mrUPW : UPW[0]
		}
		if ([0, 3, 4, 14, 8, 7].indexOf(Number(user.utype)) < 0) {
			UPW.b_index = 0
		}
		commit('setUpw', UPW)
		commit('setupwuUser', user.uUser)
		return UPW
	},
}
export default {
	state,
	getters,
	mutations,
	actions
}